package com.letv.core.parser;

import android.text.TextUtils;

import com.letv.core.bean.AlbumPayInfoBean;
import com.letv.core.bean.VideoBean;
import com.letv.core.bean.VideoBean.WatchingFocusItem;
import com.letv.core.bean.VideoFileBean;
import com.letv.core.bean.VideoPlayerBean;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 解析视频详情
 * 
 * @author zhuqiao
 *
 */
public class VideoPlayerParser extends LetvMobileParser<VideoPlayerBean> {
    private VideoPlayerBean videoPlayerBean;

    @Override
    protected VideoPlayerBean parse(JSONObject data) throws Exception {
        if (data == null) {
            return null;
        }

        videoPlayerBean = new VideoPlayerBean();

        parseVideoInfo(data.optJSONObject("videoInfo"));
        parseVideoFile(data);
        parsePay(data);

        return videoPlayerBean;
    }

    /**
     * 解析视频信息
     * 
     * @param obj
     */
    private void parseVideoInfo(JSONObject obj) {
        if (isNull(obj)) {
            return;
        }

        VideoBean video = new VideoBean();
        video.setVid(getLong(obj, "vid"));
        video.setPid(getLong(obj, "pid"));
        video.setCid(getInt(obj, "cid"));
        video.setNameCn(getString(obj, "nameCn"));
        video.setSubTitle(getString(obj, "subTitle"));
        video.setSinger(getString(obj, "singer"));
        video.setGuest(getString(obj, "guest"));
        video.setBtime(getLong(obj, "btime"));
        video.setEtime(getLong(obj, "etime"));
        video.setDuration(getLong(obj, "duration"));
        video.setMid(getString(obj, "mid"));
        video.setEpisode(getString(obj, "episode"));
        video.setPorder(getString(obj, "porder"));
        video.setPay(getInt(obj, "pay"));
        video.setDownload(getInt(obj, "download"));

        JSONObject picAll = getJSONObject(obj, "picAll");
        if (!isNull(picAll)) {
            if (has(picAll, "200*150")) {
                video.setPic(getString(picAll, "200*150"));
            } else if (has(picAll, "120*90")) {
                video.setPic(getString(picAll, "120*90"));
            } else if (has(picAll, "300*300")) {
                video.setPic300(getString(picAll, "300*300"));
            }
            if (has(picAll, "320*200")) {
                video.setPic320(getString(picAll, "320*200"));
            }
        }

        video.setPlay(getInt(obj, "play"));
        video.setJump(getInt(obj, "jump"));
        video.setJumptype(getString(obj, "jumptype"));
        video.setOpenby(getInt(obj, "openby"));
        video.setJumpLink(getString(obj, "jumplink"));
        video.setBrList(getString(obj, "brList"));
        video.setVideoTypeKey(getString(obj, "videoTypeKey"));
        video.setVideoType(getString(obj, "videoType"));
        video.setControlAreas(getString(obj, "controlAreas"));
        video.setDisableType(getInt(obj, "disableType"));

        JSONArray jsonFocusrray = null;
        ArrayList<WatchingFocusItem> watchFocusList = null;
        jsonFocusrray = getJSONArray(obj, "watchingFocus");
        if (jsonFocusrray != null && jsonFocusrray.length() > 0) {
            watchFocusList = new ArrayList<WatchingFocusItem>();
            WatchingFocusItem mWatchingFocusItem = null;
            JSONObject jsonFocusObj = null;
            int len = jsonFocusrray.length();
            for (int i = 0; i < len; i++) {
                jsonFocusObj = getJSONObject(jsonFocusrray, i);
                mWatchingFocusItem = new WatchingFocusItem();
                mWatchingFocusItem.setDesc(jsonFocusObj.optString("desc"));
                mWatchingFocusItem.setId(jsonFocusObj.optInt("id"));
                mWatchingFocusItem.setPicUrl(jsonFocusObj.optString("pic"));
                mWatchingFocusItem.setTimeDot(jsonFocusObj.optString("time"));
                watchFocusList.add(mWatchingFocusItem);
            }
            if (watchFocusList != null && watchFocusList.size() > 0) {
                video.setWatchingFocusList(watchFocusList);
            }
        }

        videoPlayerBean.setVideo(video);
    }

    /**
     * 解析调度地址
     * 
     * @param data
     */
    private void parseVideoFile(JSONObject data) throws Exception {
        // 调度地址
        VideoFileBean videoFile = new VideoFileBean();

        // ip是否屏蔽 5.8.1服务端接口没加
        videoFile.setIpEnable(status != STATE.IP_DISABLE);
        videoFile.setCountry(getString(data, "country"));

        if (has(data, "message") && status == STATE.IP_DISABLE) {
            videoFile.setMessage(getString(data, "message"));
        } else if (has(data, "videofile")) { // 调度地址
            JSONObject declineStreamObj = getJSONObject(data, "streamLevel");
            if (declineStreamObj != null) {
                videoFile.setDeclineStream("1".equals(getString(declineStreamObj, STATUS)));
                videoFile.setDeclineStreamLevel(getString(declineStreamObj, "level"));
            }

            JSONObject videoFileJson = getJSONObject(data, "videofile");
            videoFile.setMmsid(getString(videoFileJson, "mmsid"));
            videoFile.setStatus(getString(videoFileJson, STATUS));

            JSONObject videoFileInfosJson = getJSONObject(videoFileJson, "infos");
            boolean needPay = videoPlayerBean.getVideo() != null ? videoPlayerBean.getVideo().needPay() : false;

            if (has(videoFileInfosJson, "mp4_180")) {
                videoFile.setMp4_180(new VideoSchedulingAddressParser(needPay).parse(getJSONObject(videoFileInfosJson,
                        "mp4_180")));
            }
            if (has(videoFileInfosJson, "mp4_350")) {
                videoFile.setMp4_350(new VideoSchedulingAddressParser(needPay).parse(getJSONObject(videoFileInfosJson,
                        "mp4_350")));
            }
            if (has(videoFileInfosJson, "mp4_1000")) {
                videoFile.setMp4_1000(new VideoSchedulingAddressParser(needPay).parse(getJSONObject(videoFileInfosJson,
                        "mp4_1000")));
            }
            if (has(videoFileInfosJson, "mp4_1300")) {
                videoFile.setMp4_1300(new VideoSchedulingAddressParser(needPay).parse(getJSONObject(videoFileInfosJson,
                        "mp4_1300")));
            }
            if (has(videoFileInfosJson, "mp4_800_db")) {
                videoFile.setMp4_800_db(new VideoSchedulingAddressParser(needPay).parse(getJSONObject(
                        videoFileInfosJson, "mp4_800_db")));
            }
            if (has(videoFileInfosJson, "mp4_1300_db")) {
                videoFile.setMp4_1300_db(new VideoSchedulingAddressParser(needPay).parse(getJSONObject(
                        videoFileInfosJson, "mp4_1300_db")));
            }
            if (has(videoFileInfosJson, "mp4_720p_db")) {
                videoFile.setMp4_720p_db(new VideoSchedulingAddressParser(needPay).parse(getJSONObject(
                        videoFileInfosJson, "mp4_720p_db")));
            }
            if (has(videoFileInfosJson, "mp4_1080p6m_db")) {
                videoFile.setMp4_1080p6m_db(new VideoSchedulingAddressParser(needPay).parse(getJSONObject(
                        videoFileInfosJson, "mp4_1080p6m_db")));
            }
            if (has(videoFileInfosJson, "h265_flv_800")) {
                videoFile.setH265_flv_800(new VideoSchedulingAddressParser(needPay).parse(getJSONObject(
                        videoFileInfosJson, "h265_flv_800")));
            }
            if (has(videoFileInfosJson, "h265_flv_1300")) {
                videoFile.setH265_flv_1300(new VideoSchedulingAddressParser(needPay).parse(getJSONObject(
                        videoFileInfosJson, "h265_flv_1300")));
            }
            if (has(videoFileInfosJson, "h265_flv_720p")) {
                videoFile.setH265_flv_720p(new VideoSchedulingAddressParser(needPay).parse(getJSONObject(
                        videoFileInfosJson, "h265_flv_720p")));
            }
            if (has(videoFileInfosJson, "h265_flv_1080p")) {
                videoFile.setH265_flv_1080p(new VideoSchedulingAddressParser(needPay).parse(getJSONObject(
                        videoFileInfosJson, "h265_flv_1080p")));
            }

            String level = getString(videoFileInfosJson, "level");
            if (!TextUtils.isEmpty(level)) {
                if ("mp4_180".equals(level)) {
                    videoFile.setDescendLevel(videoFile.getMp4_350());
                } else if ("mp4_350".equals(level)) {
                    videoFile.setDescendLevel(videoFile.getMp4_180());
                } else if ("mp4_1000".equals(level)) {
                    videoFile.setDescendLevel(videoFile.getMp4_1000());
                } else if ("mp4_1300".equals(level)) {
                    videoFile.setDescendLevel(videoFile.getMp4_1300());
                } else if ("mp4_800_db".equals(level)) {
                    videoFile.setDescendLevel(videoFile.getMp4_800_db());
                } else if ("mp4_1300_db".equals(level)) {
                    videoFile.setDescendLevel(videoFile.getMp4_1300_db());
                } else if ("mp4_720p_db".equals(level)) {
                    videoFile.setDescendLevel(videoFile.getMp4_720p_db());
                } else if ("mp4_1080p6m_db".equals(level)) {
                    videoFile.setDescendLevel(videoFile.getMp4_1080p6m_db());
                } else if ("h265_flv_800".equals(level)) {
                    videoFile.setDescendLevel(videoFile.getH265_flv_800());
                } else if ("h265_flv_1300".equals(level)) {
                    videoFile.setDescendLevel(videoFile.getH265_flv_1300());
                } else if ("h265_flv_720p".equals(level)) {
                    videoFile.setDescendLevel(videoFile.getH265_flv_720p());
                } else if ("h265_flv_1080p".equals(level)) {
                    videoFile.setDescendLevel(videoFile.getH265_flv_1080p());
                }
            }
        }

        videoPlayerBean.setVideoFile(videoFile);
    }

    /**
     * 解析支付信息
     * 
     * @param data
     */
    private void parsePay(JSONObject data) {
        if (has(data, "payInfo")) { // 付费信息
            JSONObject payInfoJson = getJSONObject(data, "payInfo");
            AlbumPayInfoBean payInfo = new AlbumPayInfoBean();
            if (has(payInfoJson, "chargeInfo")) {
                JSONObject payChargeInfoJson = getJSONObject(payInfoJson, "chargeInfo");
                payInfo.setChargetype(getInt(payChargeInfoJson, "chargetype"));
                payInfo.setChargeflatform(getInt(payChargeInfoJson, "chargeflatform"));
                payInfo.setValidDays(getInt(payChargeInfoJson, "validDays"));
            }
            /**
             * "values": {
             * "token": "", //鉴权那边返回的TOKEN
             * "status": "0", //"0"不能播，"1"能播
             * "ticketSize": "20", //剩余观影券数量
             * "isUserBought": "0", //为用户是否购买过此影片，买过为1,没买过为0
             * },
             */
            if (has(payInfoJson, "values")) {
                JSONObject payChargeInfoJson = getJSONObject(payInfoJson, "values");
                payInfo.setIsUserBought(payChargeInfoJson.optInt("isUserBought", 0));
                payInfo.setTicketSize(payChargeInfoJson.optInt("ticketSize", -1));
                payInfo.setToken(payChargeInfoJson.optString("token", ""));
                payInfo.setIsForbidden(payChargeInfoJson.optString("isForbidden", "0"));
            }

            int defaultStatus = 1;
            if (videoPlayerBean.getVideo() != null) {
                defaultStatus = videoPlayerBean.getVideo().getPay() == 0 ? 1 : 0;
            }
            payInfo.setStatus(payInfoJson.optInt("status", defaultStatus));

            videoPlayerBean.setPayInfo(payInfo);
        }
    }
}
